import random
from random import randint

WEST = 0
NORTH = 1
EAST =2
SOUTH = 3

directions = [[-1,0], #west
              [0,1],  #north
              [1,0],  #east
              [0,-1]] #south

class room:
    def __init__(self,x,y,w,h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h

class coridor:
    def __init__(self,x,y,x1,y1):
        self.x = x
        self.y = y
        self.x1 = x1
        self.y1 = y1
        
class dang:
    
    def __init__(self):
        self.coridors = []
        self.rooms = []
        self.minLength = 10
        self.maxLength = 60
        self.roomW = 10
        self.roomH = 15
        
    def addRoom(self,der,x,y,invert=1):
        r = room(0,0,random.randint(5,self.roomW),random.randint(5,self.roomH))
        s = random.random()
        if directions[der][0] == 0: 
            r.x=x-r.w*s
            r.y=y-r.h*(1+directions[der][1]*invert)/2
        else:
            r.x=x-r.w*(1+directions[der][0]*invert)/2
            r.y=y-r.h*s
        self.rooms.append(r)

    def generateLab(self,startX,startY):
        x = startX
        y = startY
        startDer = der = random.choice([WEST,NORTH,EAST,SOUTH])
        length = random.randint(self.minLength,self.maxLength)
        i=1
        coridor = []
        coridor.append((x,y))
        while i<=length:
            size = randint(1,10)
            x+=directions[der][0]*size
            y+=directions[der][1]*size
            coridor.append((x,y))
            tempDer=der
            der = der + random.randint(-1,1) 
            if der<0:
                der = 3
            elif der>3:
                der = 0
            i+=size
        self.addRoom(startDer, startX, startY)
        self.addRoom(tempDer, coridor[-1][0], coridor[-1][1],-1)
        return coridor
            
    def generate(self,startX,startY):
        self.coridors.append(self.generateLab(startX, startY))
        for i in range(randint(1,4)): #@UnusedVariable
            s = random.choice(self.coridors[-1])
            self.coridors.append(self.generateLab(s[0], s[1]))

    def _gabarits(self):
        minx=self.rooms[0].x    
        maxx=self.rooms[0].x+self.rooms[0].w
        miny=self.rooms[0].y
        maxy=self.rooms[0].y+self.rooms[0].h
        for r in self.rooms[1:]:
            if minx>r.x: minx=r.x
            if maxx<r.x+r.w: maxx=r.x+r.w
            if miny>r.y: miny=r.y
            if maxy>r.y+r.h: maxy=r.y+r.h
        
        maxx = max(self.coridors[0]+[(maxx,0)],key=lambda x:x[0])[0]
        minx = min(self.coridors[0]+[(minx,0)],key=lambda x:x[0])[0]
        maxy = max(self.coridors[0]+[(0,maxy)],key=lambda y:y[1])[1]
        miny = min(self.coridors[0]+[(0,miny)],key=lambda y:y[1])[1]        
        for c in self.coridors[1:]:
            maxx = max(c+[(maxx,0)],key=lambda x:x[0])[0]
            minx = min(c+[(minx,0)],key=lambda x:x[0])[0]
            maxy = max(c+[(0,maxy)],key=lambda y:y[1])[1]
            miny = min(c+[(0,miny)],key=lambda y:y[1])[1]
        return [(minx,maxx),(miny,maxy)]   
 
if __name__ == "__main__":
    pass
